import importlib
import glob
from decoders.abc_decoder import abs_decoder

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow
import uidesign
app = QApplication(sys.argv)
wnd = QWidget()
uiDesign = uidesign.Ui_Form()
uiDesign.setupUi(wnd)
wnd.show()

# 搜索 xxx_utils.py
plugin_pys = [x[2:-3] for x in glob.glob('./decoders/*_decoder.py')]
# print(plugin_pys)
if len(plugin_pys) == 0:
    plugin_pys = [x[3:-3] for x in glob.glob('../decoders/*_utils.py')]
else:
    pass

# 把路径分隔符改为.号
plugin_pys = [x.replace('\\', '.') for x in plugin_pys]
plugin_pys = [x.replace('/', '.') for x in plugin_pys]        
pluginCnt = 0
# 扫描所有的 xxx_decoder.py以获取解码插件
for plugin in plugin_pys:
    a = importlib.import_module(plugin)  # 动态导入模块
    cls = a.get_class()  # 返回类名name
    if cls is None:
        continue
    dec:abs_decoder = cls()   # 实例化该类
    print(dec.strFormat)   # we have mp3, wav, numpy, csv
    uiDesign.cmbPlugins.addItem(dec.get_name())   # UI中显示
    pluginCnt += 1
    if dec.strFormat == 'csv':
        ret = dec.decode('../hm3/wav_to_np.csv', 
            column_keys = ['data1', 'data2'])
        ret = ret
    if dec.strFormat == 'mp3':
        retMp3 = dec.decode('../hm3/wokan14s.mp3', 
            column_keys = ['leftChn', 'rightChn'])
        retMp3 = retMp3
    if dec.strFormat == 'wav':
        retWav = dec.decode('../hm3/luyin.wav', 
            column_keys = ['leftChn', 'rightChn'])
        retWav = retWav
    if dec.strFormat == 'npy':
        retNpy = dec.decode('none', 
            column_keys = ['col1', 'col2', 'col3'])
        retNpy = retNpy
app.exec_()
